//
//  RevisePwdViewController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/16.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "RevisePwdViewController.h"
#import "addAdressCell.h"
#import "LoginViewController.h"
#import "LoginStore.h"

@interface RevisePwdViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UserDefaultsManager *usermanager;
@property (nonatomic, copy) NSString *passWord;
@property (nonatomic, copy) NSString *rePassWord;

@end

@implementation RevisePwdViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self CleanSubView];
}
- (void)CleanSubView{
    NSLog(@"%@",self.navigationController.viewControllers);
    self.usermanager = [UserDefaultsManager sharedInstance];
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    ViewHeader *header = [[ViewHeader alloc]initWithDefaultString:@"修改密码"];
    [self.view addSubview:header];
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, iPhoneX_Top, SCREEN_WIDTH, 100) style:UITableViewStyleGrouped];
    tableView.scrollEnabled = NO;
    [tableView registerNib:[UINib nibWithNibName:@"addAdressCell" bundle:nil] forCellReuseIdentifier:@"addAdressCellID"];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    UIButton *tosure = [[UIButton alloc]init];
    tosure.backgroundColor = [UIColor colorWithHexString:@"0CA6F0"];
    tosure.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
    [tosure setTitle:@"确定" forState:UIControlStateNormal];
    [tosure setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    [tosure addTarget:self action:@selector(tosure) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tosure];
    [tosure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(49);
        make.left.equalTo(self.view.mas_left).offset(14);
        make.right.equalTo(self.view.mas_right).offset(-14);
        make.top.equalTo(tableView.mas_bottom).offset(50);
    }];
}
- (void)tosure{
    NSMutableDictionary *prams = [[NSMutableDictionary alloc]init];
    [prams setObject:self.phone forKey:@"userName"];
    if (self.passWord.length > 0) {
        if (self.rePassWord.length > 0) {
            if ([[self.rePassWord stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:[self.passWord stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]) {
                [prams setObject:[self.passWord stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"passWord"];
                [prams setObject:[self.rePassWord stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"rePassWord"];
                [self forgetWith:prams];
            }else{
                [self showMBPError:@"两次密码不一致"];
            }
        }else{
            [self showMBPError:@"请再次输入密码"];
        }
    }else{
        [self showMBPError:@"请输入密码"];
    }
}
- (void)forgetWith:(NSDictionary *)prams{
    [[[LoginStore alloc]init] forgetPassword:prams Success:^(NSString * _Nonnull code) {
        if ([code isEqualToString:@"success"]) {
            [self showMBPError:@"修改成功"];
            [[NSUserDefaults standardUserDefaults] setObject:self.passWord forKey:@"pwd"];
            self.usermanager.user_pwd = self.passWord;
            [self performSelector:@selector(poploginView) withObject:nil afterDelay:1.0f];
        }else{
            [self showMBPError:code];
        }
    } Failure:nil];
}
- (void)poploginView{
    UIViewController *Controller = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 3];
    if ([Controller isKindOfClass:[LoginViewController class]]) {
        LoginViewController *VC = (LoginViewController *)Controller;
        [self.navigationController popToViewController:VC animated:YES];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    addAdressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addAdressCellID"];
    cell.rightarrow.hidden = YES;
    cell.detailtext.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (indexPath.row == 0) {
        cell.subtitle.text = @"新密码";
        cell.detailtext.placeholder = @"请输入新密码";
    }else if (indexPath.row == 1) {
        cell.subtitle.text = @"确认";
        cell.detailtext.placeholder = @"请再次输入密码";
    }
    [cell setDetailBlock:^(NSString * _Nonnull str) {
        if (indexPath.row == 0) {
            self.passWord = str;
        }else if (indexPath.row == 1){
            self.rePassWord = str;
        }
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
